package com.lb.shopguide.ui.fragment.guide.member;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.index.AdapterIndexMember;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.ChooseMemberBean;
import com.lb.shopguide.event.guide.SelectMemberEvent;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.ui.dialog.DialogProgressBar;
import com.lb.shopguide.ui.fragment.guide.FragmentSearch;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.yokeyword.indexablerv.IndexableAdapter;
import me.yokeyword.indexablerv.IndexableLayout;
import me.yokeyword.indexablerv.SimpleHeaderAdapter;

/**
 * Created by kang on 2017/6/9.
 */

public class FragmentSelectMember extends BaseCommonFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.layout_search)
    LinearLayout layoutSearch;
    @BindView(R.id.indexableLayout)
    IndexableLayout indexableLayout;

    private DialogProgressBar dialogProgressBar;
    private List<ChooseMemberBean> mChooseMemberList;
    private AdapterIndexMember mAdapterIndexMember;
    private SimpleHeaderAdapter mVisitorAdapter;

    private String checkoutType;

    public static FragmentSelectMember newInstance(String checkoutType) {
        FragmentSelectMember fragment = new FragmentSelectMember();
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.CHECKOUT_TYPE, checkoutType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_select_member;
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.CHECKOUT_TYPE)) {
            checkoutType = bundle.getString(AppConstant.CHECKOUT_TYPE);
        }
        mChooseMemberList = new ArrayList<>();
        mUserConfigManager = UserConfigManager.getInstance(mContext);
        sendRequestGetMemberList();
    }

    @Override
    public void initView() {
        ntb = view.findViewById(R.id.ntb);
        layoutSearch = view.findViewById(R.id.layout_search);
        indexableLayout = view.findViewById(R.id.indexableLayout);

        ntb.setTitleText("选择会员");
        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        indexableLayout.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapterIndexMember = new AdapterIndexMember(mContext);
        indexableLayout.setAdapter(mAdapterIndexMember);
        mAdapterIndexMember.setDatas(mChooseMemberList);
        indexableLayout.setCompareMode(IndexableLayout.MODE_FAST);

        dialogProgressBar = DialogProgressBar.getDialogProgressBar();
        dialogProgressBar.show(getFragmentManager(), getPageTag());
        layoutSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(FragmentSearch.newInstance(AppConstant.SEARCH_MEMBER));
            }
        });
        mAdapterIndexMember.setOnItemContentClickListener(new IndexableAdapter.OnItemContentClickListener<ChooseMemberBean>() {
            @Override
            public void onItemClick(View v, int originalPosition, int currentPosition, ChooseMemberBean entity) {
                pop();
                EventBus.getDefault().post(new SelectMemberEvent(entity));
            }
        });
    }

    @Override
    public void finishRefresh() {
        dialogProgressBar.dismiss();
    }

    @Override
    public void sessionOutOfDate() {
        ((GuideMainActivity) getActivity()).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
        finishRefresh();
    }

    @Override
    public String getPageTag() {
        return FragmentSelectMember.class.getName();
    }

    private void initVisitorHeader() {
        mVisitorAdapter = new SimpleHeaderAdapter<>(mAdapterIndexMember, "游", "游客", initVisitorDatas());
        indexableLayout.addHeaderAdapter(mVisitorAdapter);
    }

    private List<ChooseMemberBean> initVisitorDatas() {
        List<ChooseMemberBean> list = new ArrayList<>();
        ChooseMemberBean bean = new ChooseMemberBean();
        bean.setTrueName("游客");
        bean.setMemberCode("");
        bean.setHasCoupons(false);
        list.add(bean);
        return list;
    }


    private void sendRequestGetMemberList() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                JsonArray jsonMembers = JsonUtil.getJsonArrayFromMap(response.getReturnContent());
                if (jsonMembers.size() != 0) {
                    for (JsonElement obj : jsonMembers) {
                        ChooseMemberBean bean = JsonUtil.getGson().fromJson(obj, ChooseMemberBean.class);
                        mChooseMemberList.add(bean);
                    }
                }
                mAdapterIndexMember.setDatas(mChooseMemberList);
                finishRefresh();
                initVisitorHeader();
            }
        };
        ApiMethodGuide.getMemberList(new SimpleLbObserver(listener, otherListener), mUserConfigManager.getStoreCode(), null, this, FragmentEvent.DESTROY_VIEW);
    }

}
